// #include "client.h"
#include "client_api.h"

ClientContext ctx;



int main() {
    int sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd < 0) {
        perror("套接字创建失败");
        exit(EXIT_FAILURE);
    }

    struct sockaddr_in serv_addr = {.sin_family = AF_INET, .sin_port = htons(SERVER_PORT)};
    if (inet_pton(AF_INET, SERVER_IP, &serv_addr.sin_addr) <= 0) {
        perror("地址无效");
        exit(EXIT_FAILURE);
    }
    if (connect(sockfd, (const struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
        perror("连接失败");
        exit(EXIT_FAILURE);
    }

    ctx.sockfd = sockfd;
    pthread_mutex_init(&ctx.mutex, NULL);
    pthread_cond_init(&ctx.cond, NULL);
    ctx.socket_closed = false;

    pthread_t tid;
    pthread_create(&tid, NULL, receiver_thread, NULL);
    pthread_detach(tid);

    // // 启动心跳线程
    pthread_t heartbeat_tid;
    pthread_create(&heartbeat_tid, NULL, heartbeat_thread, NULL);
    pthread_detach(heartbeat_tid);

    while(1) {
        send_register("world", 5);
        usleep(100000);
    }

    close(sockfd);
    pthread_mutex_destroy(&ctx.mutex);
    pthread_cond_destroy(&ctx.cond);
    return 0;
}
